package com.codeczx.umengdemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.umeng.analytics.MobclickAgent;

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPermission();
    }

    private void getPermission() {
        //判断系统版本，6.0需要运行时获取权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkWritePermission = ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_PHONE_STATE);
            if (checkWritePermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 0);
            } else {
                MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
                Log.i("codeczx","have permission setType");
            }
        } else {
            MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
            Log.i("codeczx","<6.0 setType");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0){
            if(resultCode==RESULT_OK){
                MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
                Log.i("codeczx","onActivityResult setType");
            }
        }
    }
}
